
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px" v-loading="loading">
                             <el-form-item label="姓名" prop="name">
                        <el-input v-model.trim="data.name" placeholder="请填写姓名" />
                    </el-form-item>
                    <el-form-item label="手机号" prop="mobile">
                        <el-input v-model.trim="data.mobile" placeholder="请填写手机号" />
                    </el-form-item>
               
                    <el-form-item label="余额" prop="balance">
                        <el-input v-model.trim="data.balance" readonly placeholder="请填写余额" />
                    </el-form-item>
                    <el-form-item label="分销比例" prop="percent">
                        <el-input v-model.trim="data.percent" readonly placeholder="请填写分销比例（百分比）" />
                    </el-form-item>
  
  
          <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
          </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {detail,edit} from '@/api/distributor'
import { getToken } from '@/utils/auth'



export default {
  name: 'distributorCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },

  },
  data() {
    return {
      loading:true,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
        'Authorization': 'Bearer ' + getToken()
        },
      rules: {
        
      },
      data: {
                        name:'',
            mobile:'',
            percent:'',
            status:'',
            balance:'',

         }
    }
  },
  created() {
    const id = this.item.id
    this.id = id
    this.fetchData(id)
  },
  watch: {
    item: {
      handler(val, oldVal) {
        if (this.item.dialogVisible === true 
        ) {
          this.id = val.id
          this.fetchData(val.id)
        }
      },
      deep: true
    }
  },
  methods: {
    
    fetchData(id) {
          detail(id).then(response => {
            this.loading = false;
            this.data = response.data;
            
          }).catch(err => {
            console.log(err)
          })
        },
    submit() {
      const thiz = this
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        edit({
          id: this.data.id,
             name:this.data.name,
            mobile:this.data.mobile,
            percent:this.data.percent,
            status:this.data.status,
            balance:this.data.balance,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '编辑成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
